import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lighting_driver/src/moduls/search/search_view.dart';

import '../../../../generated/my_localizations.dart';
import '../../../const/const.dart';
import '../controllers/home_controller.dart';

class MyDrawer extends StatefulWidget {
  bool isLeft;
  MyDrawer({super.key, required this.isLeft});

  @override
  State<MyDrawer> createState() => _MyDrawerState();
}

class _MyDrawerState extends State<MyDrawer> {
  final HomeController controller = Get.find<HomeController>();
  late final IconData headIcon;
  late final List<IconData> icons;

  bool openMyDevice = false;
  @override
  void initState() {
    super.initState();
    icons = widget.isLeft ? [Icons.lock, Icons.lightbulb_outlined] : [Icons.error_outline];
    headIcon = widget.isLeft ? Icons.more_horiz : Icons.settings;
  }

  @override
  Widget build(BuildContext context) {
    final localizations = MyLocalizations.of(context);
    final titles =
        widget.isLeft ? [localizations?.password ?? "", localizations?.my_devices ?? ''] : [localizations?.about ?? ''];

    return Drawer(
      backgroundColor: bgColor,
      child: IntrinsicHeight(
        child: Column(
          children: [
            SafeArea(
              bottom: false,
              child: ListView.builder(
                  // physics: NeverScrollableScrollPhysics(),
                  shrinkWrap: true, // 💥 关键点！// 子内容高度撑开 ListView
                  itemCount: titles.length + 1,
                  padding: EdgeInsets.only(bottom: 0),
                  itemBuilder: (BuildContext context, int index) {
                    ///三点
                    if (index == 0) {
                      return Align(
                        alignment: widget.isLeft ? Alignment.centerRight : Alignment.centerLeft,
                        child: IconButton(
                          icon: Icon(headIcon),
                          onPressed: () => Navigator.pop(context),
                        ),
                      );
                    }

                    ///文字列表
                    else {
                      return Container(
                        decoration: const BoxDecoration(
                          border: Border(bottom: BorderSide(color: Colors.grey, width: 0.5)),
                          // color: Colors.blue,
                        ),
                        child: ListTile(
                            contentPadding: const EdgeInsets.only(left: 10, right: 10),
                            title: Text(
                              titles[index - 1],
                              textAlign: TextAlign.left,
                              style: const TextStyle(color: Colors.white),
                            ),
                            leading: Icon(icons[index - 1], color: Colors.white, size: 22),
                            trailing: Icon(
                                widget.isLeft && index == titles.length && openMyDevice
                                    ? Icons.keyboard_arrow_down
                                    : Icons.keyboard_arrow_right,
                                color: Colors.white,
                                size: 34),
                            onTap: () {
                              if (index == titles.length) {
                                setState(() => openMyDevice = !openMyDevice);
                              }
                              controller.clickTitle(context, titles[index - 1]);
                            }),
                      );
                    }
                  }),
            ),

            ///
            !openMyDevice || !widget.isLeft ? Container() : const Expanded(child: SearchView()),
          ],
        ),
      ),
    );
  }
}
